﻿var updated;
function getHiddenProp(){
    var prefixes = ['webkit','moz','ms','o'];
    
    // if 'hidden' is natively supported just return it
    if ('hidden' in document) return 'hidden';
    
    // otherwise loop over all the known prefixes until we find one
    for (var i = 0; i < prefixes.length; i++){
        if ((prefixes[i] + 'Hidden') in document) 
            return prefixes[i] + 'Hidden';
    }

    // otherwise it's not supported
    return null;
}
function isHidden() {
    var prop = getHiddenProp();
    if (!prop) return false;
    
    return document[prop];
}
var visProp = getHiddenProp();
if (visProp) {
  var evtname = visProp.replace(/[H|h]idden/,'') + 'visibilitychange';
  document.addEventListener(evtname, visChange);
}

function visChange() {
      if (isHidden())
         updated = false;
      else
         updateTime();
}


// self.onblur = function() { updated = false; }
// self.onfocus = function() { updateTime() }

function updateTime() {
	if (updated == true) {
		current_seconds += 1000/10;

		if (current_seconds >= 60) {
			//console.log(current_seconds)
			currentTime = new Date;
			currentTime.setHours(currentTime.getHours() + hoursOffset);
			currentTime.setMinutes(currentTime.getMinutes() + minutesOffset);
			currentTime.setSeconds(currentTime.getSeconds() + secondsOffset);
			current_seconds = currentTime.getSeconds();
			current_minutes = currentTime.getMinutes();
			current_hours = currentTime.getHours();
			current_milliseconds = currentTime.getMilliseconds();
		}
		//console.log(currentTime)
	} else {
		currentTime = new Date;
		// console.log(current_seconds)
		currentTime.setHours(currentTime.getHours() + hoursOffset);
		currentTime.setMinutes(currentTime.getMinutes() + minutesOffset);
		currentTime.setSeconds(currentTime.getSeconds() + secondsOffset);
		monthdate = currentTime.getDate();
		weekday_num = currentTime.getDay();
		current_seconds = currentTime.getSeconds();
		current_minutes = currentTime.getMinutes();
		current_hours = currentTime.getHours();
		current_milliseconds = currentTime.getMilliseconds();
		// console.log(current_seconds,currentTime.getMilliseconds())
		updated = true;
	}

}
function setTimeToDefaultTime() {
currentTime = new Date;
currentTime.setHours(10);
currentTime.setMinutes(11);
currentTime.setSeconds(31);
monthdate = currentTime.getDate();
weekday_num = currentTime.getDay();
current_seconds = currentTime.getSeconds();
current_minutes = currentTime.getMinutes();
current_hours = currentTime.getHours();
current_milliseconds = currentTime.getMilliseconds()
}
function updateTimeCalculations() {
var a = current_hours;
weekday_num++;
if (a > 12) {
a -= 12
}
display_calculation["seconds_base_60"] = current_seconds * timeSlice;
display_calculation["seconds_base_60"] += current_milliseconds * timeSlice / 1e3;
display_calculation["seconds_base_60"] += rotCorrect;
display_calculation["minutes_base_60"] = current_minutes * timeSlice;
display_calculation["minutes_base_60"] += current_seconds * timeSlice / 60;
display_calculation["minutes_base_60"] += rotCorrect;
display_calculation["minutes_base_30"] = current_minutes * timeSlice * 2;
display_calculation["minutes_base_30"] += current_seconds * timeSlice * 2 / 60;
display_calculation["minutes_base_30"] += rotCorrect;
display_calculation["hours_base_12"] = a * timeSlice * 5;
display_calculation["hours_base_12"] += current_minutes * timeSlice / 12;
display_calculation["hours_base_12"] += rotCorrect;
display_calculation["monthdate_base_31"] = monthdate * (timeSlice / 6) * (360 / 31);
display_calculation["monthdate_base_31"] += rotCorrect
}
function easeInOutQuad(a, b, c, d) {
if (a < d / 2) return 2 * c * a * a / (d * d) + b;
ts = a - d / 2;
return -2 * c * ts * ts / (d * d) + 2 * c * ts / d + c / 2 + b
}
function setHoursOffset(a) {
hoursOffset = a
}
function setMinutesOffset(a) {
minutesOffset = a
}
function setSecondsOffset(a) {
secondsOffset = a
}
function getCurrentShownTime() {
var a = "";
if (current_hours < 10) {
a += "0"
}
a += current_hours;
a += ":";
if (current_minutes < 10) {
a += "0"
}
a += current_minutes;
return a
}
function updateOnTimeDisplayOnce() {
updateTimeCalculations();
updateDisplay()
}
function updateOnTimeDisplayOvertime() {
updateTime();
updateOnTimeDisplayOnce();
requestAnimFrame(updateOnTimeDisplayOvertime);
}
var ticks_per_seconds = 10;
var parts_starting_z_index = 23;
var tickDelay = 1e3 / ticks_per_seconds;
var timeSlice = Math.PI * 2 / 60;
var rotCorrect = global_watch_p["rotation_angle"] * (timeSlice / 6);
var display_calculation = new Array;
var currentTime = null;
var hoursOffset = 0;
var minutesOffset = 0;
var secondsOffset = 0;
var monthdate = null;
var weekday_num = null;
var current_seconds = null;
var current_minutes = null;
var current_hours = null;
var current_milliseconds = null